package ecnu.liwaTest.dd.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import java.util.*;

import static ecnu.liwaTest.dd.DriverSingleton.waitForWindow;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

public class ZxsPage extends BasePage {

    public ZxsPage goZxsPage() {
        driver.findElement(By.cssSelector(".el-menu-item:nth-child(5) > span")).click();
        waitForWindow(1000);
        assertThat(driver.getTitle(), is("咨询师管理"));
        return this;
    }

    public ZxsPage arrangeZxs() {
        driver.findElement(By.cssSelector(".el-button--default:nth-child(2) > span")).click();
        {
            WebElement element = driver.findElement(By.cssSelector(".el-button--default:nth-child(2) > span"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element).perform();
        }
        {
            WebElement element = driver.findElement(By.tagName("body"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element, 0, 0).perform();
        }
        driver.findElement(By.cssSelector(".arrangementRow > .el-button:nth-child(1) > span")).click();
        driver.findElement(By.cssSelector(".el-dialog__footer:nth-child(3) .confirm")).click();
        return this;
    }

    public ZxsPage changeBlockState() {
        driver.findElement(By.cssSelector(".el-button:nth-child(3) > span")).click();
        waitForWindow(500);
        driver.findElement(By.cssSelector(".el-button--small:nth-child(2) > span")).click();
        return this;
    }

    public ZxsPage zxsState(String state) {
        waitForWindow(1000);
        String currState = driver.findElement(By.cssSelector(".el-table_2_column_12 span")).getText();
        assertThat(currState, is(state));
        return this;
    }

    public ZxsPage changeName(String originalName, String changedName) {
        searchName(originalName);
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".tableBtn:nth-child(1) > span")).click();
        driver.findElement(By.cssSelector(".el-row:nth-child(1) > .el-col:nth-child(1) > .el-form-item .el-input__inner")).click();
        //driver.findElement(By.cssSelector(".el-row:nth-child(1) > .el-col:nth-child(1) > .el-form-item .el-input__inner")).clear();
        driver.findElement(By.cssSelector(".el-row:nth-child(1) > .el-col:nth-child(1) > .el-form-item .el-input__inner")).sendKeys(Keys.CONTROL, "a");
        driver.findElement(By.cssSelector(".el-row:nth-child(1) > .el-col:nth-child(1) > .el-form-item .el-input__inner")).sendKeys(Keys.DELETE);
        if(!"".equals(changedName)) {
            driver.findElement(By.cssSelector(".el-row:nth-child(1) > .el-col:nth-child(1) > .el-form-item .el-input__inner")).sendKeys(changedName);
            driver.findElement(By.cssSelector(".el-row:nth-child(1) > .el-col:nth-child(1) > .el-form-item .el-input__inner")).sendKeys(Keys.ENTER);
        }
        driver.findElement(By.cssSelector(".el-row:nth-child(1) > .el-col:nth-child(2) > .el-form-item .el-input__inner")).click();
        waitForWindow(5000);
        driver.findElement(By.cssSelector(".el-dialog__footer:nth-child(3) .confirm")).click();
        return this;
    }

    public ZxsPage changePhone(String name, String phoneNum) {
        searchName(name);
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".tableBtn:nth-child(1) > span")).click();
        driver.findElement(By.cssSelector(".el-row:nth-child(1) > .el-col:nth-child(2) > .el-form-item .el-input__inner")).click();
        //driver.findElement(By.cssSelector(".el-row:nth-child(1) > .el-col:nth-child(2) > .el-form-item .el-input__inner")).clear();
        driver.findElement(By.cssSelector(".el-row:nth-child(1) > .el-col:nth-child(2) > .el-form-item .el-input__inner")).sendKeys(Keys.CONTROL, "a");
        driver.findElement(By.cssSelector(".el-row:nth-child(1) > .el-col:nth-child(2) > .el-form-item .el-input__inner")).sendKeys(Keys.DELETE);
        if(!"".equals(phoneNum)) {
            driver.findElement(By.cssSelector(".el-row:nth-child(1) > .el-col:nth-child(2) > .el-form-item .el-input__inner")).sendKeys(phoneNum);
            driver.findElement(By.cssSelector(".el-row:nth-child(1) > .el-col:nth-child(2) > .el-form-item .el-input__inner")).sendKeys(Keys.ENTER);
        }
        driver.findElement(By.className("dialogTitle")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".el-dialog__footer:nth-child(3) .confirm")).click();
        return this;
    }

    public ZxsPage changeId(String name, String id) {
        searchName(name);
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".tableBtn:nth-child(1) > span")).click();
        driver.findElement(By.cssSelector(".el-row:nth-child(2) .el-input__inner")).click();
        //driver.findElement(By.cssSelector(".el-row:nth-child(2) .el-input__inner")).clear();
        driver.findElement(By.cssSelector(".el-row:nth-child(2) .el-input__inner")).sendKeys(Keys.CONTROL, "a");
        driver.findElement(By.cssSelector(".el-row:nth-child(2) .el-input__inner")).sendKeys(Keys.DELETE);
        if (!"".equals(id)) {
            driver.findElement(By.cssSelector(".el-row:nth-child(2) .el-input__inner")).sendKeys(id);
            driver.findElement(By.cssSelector(".el-row:nth-child(2) .el-input__inner")).sendKeys(Keys.ENTER);
        }
        driver.findElement(By.className("dialogTitle")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".el-dialog__footer:nth-child(3) .confirm")).click();
        return this;
    }

    public ZxsPage changeEmail(String name, String newEmail) {
        searchName(name);
        driver.findElement(By.cssSelector(".tableBtn:nth-child(1) > span")).click();
        driver.findElement(By.cssSelector(".el-row:nth-child(3) .changeInput > .el-input__inner")).click();
        //driver.findElement(By.cssSelector(".el-row:nth-child(3) .changeInput > .el-input__inner")).clear();
        driver.findElement(By.cssSelector(".el-row:nth-child(3) .changeInput > .el-input__inner")).sendKeys(Keys.CONTROL, "a");
        driver.findElement(By.cssSelector(".el-row:nth-child(3) .changeInput > .el-input__inner")).sendKeys(Keys.DELETE);
        if(!"".equals(newEmail)) {
            driver.findElement(By.cssSelector(".el-row:nth-child(3) .changeInput > .el-input__inner")).sendKeys(newEmail);
            driver.findElement(By.cssSelector(".el-row:nth-child(3) .changeInput > .el-input__inner")).sendKeys(Keys.ENTER);
        }
        driver.findElement(By.className("dialogTitle")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".el-dialog__footer:nth-child(3) .confirm")).click();
        return this;
    }

    public ZxsPage changeDd(String name, String ddName) {
        searchName(name);
        driver.findElement(By.cssSelector(".tableBtn:nth-child(1) > span")).click();
        waitForWindow(500);

        driver.findElement(By.cssSelector(".el-input:nth-child(2) .el-select__caret")).click();
        driver.findElement(By.cssSelector(".el-select__input")).click();
        waitForWindow(1000);

        if(!"".equals(ddName)) {
            List<WebElement> elementList = driver.findElements(By.className("el-select-dropdown__item"));
            for (WebElement element : elementList) {
                if (element.getText().contains(ddName)) {
                    //driver.findElement(By.cssSelector(".hover > span")).click();
                    element.click();
                    break;
                }
            }
        }
        driver.findElement(By.className("dialogTitle")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".el-dialog__footer:nth-child(3) .confirm")).click();
        return this;
    }

    public ZxsPage changeCompany(String name, String company) {
        searchName(name);
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".tableBtn:nth-child(1) > span")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".el-row:nth-child(4) > .el-col:nth-child(1) .el-input__inner")).click();
        //driver.findElement(By.cssSelector(".el-row:nth-child(4) > .el-col:nth-child(1) .el-input__inner")).clear();
        driver.findElement(By.cssSelector(".el-row:nth-child(4) > .el-col:nth-child(1) .el-input__inner")).sendKeys(Keys.CONTROL, "a");
        driver.findElement(By.cssSelector(".el-row:nth-child(4) > .el-col:nth-child(1) .el-input__inner")).sendKeys(Keys.DELETE);
        if(!"".equals(company)) {
            driver.findElement(By.cssSelector(".el-row:nth-child(4) > .el-col:nth-child(1) .el-input__inner")).sendKeys(company);
            driver.findElement(By.cssSelector(".el-row:nth-child(4) > .el-col:nth-child(1) .el-input__inner")).sendKeys(Keys.ENTER);
        }
        driver.findElement(By.cssSelector(".el-dialog__footer:nth-child(3) .confirm")).click();

        return this;
    }

    public ZxsPage changeJob(String name, String job) {
        searchName(name);
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".tableBtn:nth-child(1) > span")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".el-row:nth-child(4) > .el-col:nth-child(2) .el-input__inner")).click();
        //driver.findElement(By.cssSelector(".el-row:nth-child(4) > .el-col:nth-child(2) .el-input__inner")).clear();
        driver.findElement(By.cssSelector(".el-row:nth-child(4) > .el-col:nth-child(2) .el-input__inner")).sendKeys(Keys.CONTROL, "a");
        driver.findElement(By.cssSelector(".el-row:nth-child(4) > .el-col:nth-child(2) .el-input__inner")).sendKeys(Keys.DELETE);
        if(!"".equals(job)) {
            driver.findElement(By.cssSelector(".el-row:nth-child(4) > .el-col:nth-child(2) .el-input__inner")).sendKeys(job);
            waitForWindow(1000);
            driver.findElement(By.cssSelector(".el-row:nth-child(4) > .el-col:nth-child(2) .el-input__inner")).sendKeys(Keys.ENTER);
        }
        driver.findElement(By.cssSelector(".el-dialog__footer:nth-child(3) .confirm")).click();

        return this;
    }

    public ZxsPage changeUsername(String name, String userId) {
        searchName(name);
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".tableBtn:nth-child(1) > span")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".el-row:nth-child(5) > .el-col:nth-child(1) .el-input__inner")).click();
        //driver.findElement(By.cssSelector(".el-row:nth-child(5) > .el-col:nth-child(1) .el-input__inner")).clear();
        driver.findElement(By.cssSelector(".el-row:nth-child(5) > .el-col:nth-child(1) .el-input__inner")).sendKeys(Keys.CONTROL, "a");
        driver.findElement(By.cssSelector(".el-row:nth-child(5) > .el-col:nth-child(1) .el-input__inner")).sendKeys(Keys.DELETE);
        if(!"".equals(userId)) {
            waitForWindow(1000);
            driver.findElement(By.cssSelector(".el-row:nth-child(5) > .el-col:nth-child(1) .el-input__inner")).sendKeys(userId);
            driver.findElement(By.cssSelector(".el-row:nth-child(5) > .el-col:nth-child(1) .el-input__inner")).sendKeys(Keys.ENTER);
        }
        driver.findElement(By.cssSelector(".el-dialog__footer:nth-child(3) .confirm")).click();

        return this;
    }

    public ZxsPage changePassword(String name, String password) {
        searchName(name);
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".tableBtn:nth-child(1) > span")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".el-row:nth-child(6) > .el-col:nth-child(1) .el-input__inner")).click();
        //driver.findElement(By.cssSelector(".el-row:nth-child(6) > .el-col:nth-child(1) .el-input__inner")).clear();
        driver.findElement(By.cssSelector(".el-row:nth-child(6) > .el-col:nth-child(1) .el-input__inner")).sendKeys(Keys.CONTROL, "a");
        driver.findElement(By.cssSelector(".el-row:nth-child(6) > .el-col:nth-child(1) .el-input__inner")).sendKeys(Keys.DELETE);
        if(!"".equals(password)) {
            waitForWindow(1000);
            driver.findElement(By.cssSelector(".el-row:nth-child(6) > .el-col:nth-child(1) .el-input__inner")).sendKeys(password);
            driver.findElement(By.cssSelector(".el-row:nth-child(6) > .el-col:nth-child(1) .el-input__inner")).sendKeys(Keys.ENTER);
        }
        driver.findElement(By.cssSelector(".el-dialog__footer:nth-child(3) .confirm")).click();

        return this;
    }

    public ZxsPage verifyDd(String ddName) {
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".tableBtn:nth-child(1) > span")).click();
        waitForWindow(1000);
        List<WebElement> elementList = driver.findElements(By.cssSelector(".el-select__tags-text"));
        List<String> nameStr = new ArrayList<>();
        for(WebElement element:elementList) {
            nameStr.add(element.getText());
        }
        driver.findElement(By.cssSelector(".el-dialog__footer:nth-child(3) .cancel")).click();
        assertThat(nameStr, everyItem(containsString(ddName)));
        return this;
    }

    public ZxsPage verifyEmail(String email) {
        waitForWindow(1000);
        String text = driver.findElement(By.cssSelector(".el-table_2_column_10 span")).getText();
        assertThat(text, is(email));
        return this;
    }

    public ZxsPage verifyPhone(String phoneNum) {
        waitForWindow(1000);
        String text = driver.findElement(By.cssSelector(".el-table_2_column_9 span")).getText();
        assertThat(text, is(phoneNum));
        return this;
    }

    public ZxsPage verifyGender(String id) {
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".tableBtn:nth-child(1) > span")).click();
        driver.findElement(By.cssSelector(".el-row:nth-child(2) .el-input__inner")).click();
//        String id = driver.findElement(By.cssSelector(".el-row:nth-child(2) .el-input__inner")).getText();
        String gender = driver.findElement(By.cssSelector(".genderActive")).getText();
        driver.findElement(By.cssSelector(".el-dialog__footer:nth-child(3) .cancel")).click();
        if(Integer.parseInt(String.valueOf(id.charAt(17)))%2 == 0) {
            assertThat(gender, is("女"));
        }
        else{
            assertThat(gender, is("男"));
        }
        return this;
    }

    public ZxsPage verifyAge(String id) {
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".tableBtn:nth-child(1) > span")).click();
        driver.findElement(By.cssSelector(".el-row:nth-child(2) .el-input__inner")).click();
        int ageGet = Integer.parseInt(driver.findElement(By.cssSelector(".customAge")).getText());
        driver.findElement(By.cssSelector(".el-dialog__footer:nth-child(3) .cancel")).click();
        Calendar born = Calendar.getInstance();
        born.set(Calendar.YEAR, Integer.parseInt(id.substring(6, 10)));
        born.set(Calendar.MONTH, Integer.parseInt(id.substring(10, 12)));
        born.set(Calendar.DATE, Integer.parseInt(id.substring(12, 14)));
        Calendar now = Calendar.getInstance();
        now.set(Calendar.MONTH, now.get(Calendar.MONTH) + 1);

        int age = now.get(Calendar.YEAR) - born.get(Calendar.YEAR);
        if(now.get(Calendar.MONTH) < born.get(Calendar.MONTH)) {
            age--;
        }
        else if(now.get(Calendar.MONTH) == born.get(Calendar.MONTH)) {
            if(now.get(Calendar.DATE) < born.get(Calendar.DATE)) {
                age--;
            }
        }

        assertThat(ageGet, is(age));
        return this;
    }

//    public ZxsPage nameError() {
//        waitForWindow(1000);
//        String text = driver.findElement(By.cssSelector(".el-form-item__error")).getText();
//        assertThat(text, is("请检查姓名输入是否正确"));
//        return this;
//    }
//
//    public ZxsPage phoneError(String errorText) {
//        waitForWindow(1000);
//        String text = driver.findElement(By.cssSelector(".el-col:nth-child(2) .el-form-item__error")).getText();
//        assertThat(text, is(errorText));
//        return this;
//    }

//    public ZxsPage getError(String errorText) {
//        waitForWindow(1000);
//        String text = driver.findElement(By.cssSelector(".el-form-item__error")).getText();
//        assertThat(text, is(errorText));
//        return this;
//    }
}
